<?php
namespace Admin\Controller;

class SiteController extends Controller {

    public function index() {
        $m    = D('Site');
        $page = $m->where($this->indexExpr)->join($join)->order($this->sortExpr)->page2();

        $this->assign('page', $page)
             ->display();
    }

    public function add() {
        $uid = I('get.uid', 0, 'intval');

        if (empty($uid)) {
            throw new \Exception('没有站长编号，无法增加网站');
        }

        if (!$record = D('Siter')->getById($uid)) {
            throw new \Exception('没有找到站长信息');
        }

        $r = array(
            'uid' => $record['id'],
        );

        $this->assign('record', $r);
        $this->display();
    }

    public function modify() {
        $id = I('get.id', 0, 'intval');

        if (empty($id)) {
            throw new \Exception('参数不完整，无法获取数据');
        }

        $m = D2();

        if (!$record = $m->getById($id)) {
            throw new \Exception('没有找到网站信息');
        }

        $this->assign('record', $record);
        $this->display();
    }

    public function doSave() {
        $d = $_POST;
        $m = D2();

        try {
            $r = $m->save2($d) 
                 ? ['s' => true,  'm' => '保存成功']
                 : ['s' => false, 'm' => '保存出错'];
        } catch (\Exception $e) {
            $r = ['s' => false, 'm' => $e->getMessage()];
        }

        showJson($r);
    }

    public function del() {
        $id = I('get.id', 0, 'intval');

        if (empty($id)) {
            showJson(['s' => false, 'm' => '参数不完整，无法删除数据']);
        }

        $m = D2();
        $r = $m->del($id) 
             ? ['s' => true,  'm' => '删除成功']
             : ['s' => false, 'm' => '删除出错'];

        showJson($r);
    }

}
